package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.MatchOnlineRecordPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ljb
 * @date 2022/10/08 16:39
 **/
public class MatchOnlineRecordDAO {
    private static final String
            REPLACE_ONE = "replace into `matchonlinerecord` (`pCode`,`code`, `infoA`,`infoB`,`isWin`, `battleResult`,`date`, `recordCodes`, `syncdb`, `updateTime`) values (?,?,?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `matchonlinerecord` where `pCode` = ?",
            DELETE = "delete from `matchonlinerecord` where `pCode` = ? and `updateTime` != ?";

    public static List<MatchOnlineRecordPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, MatchOnlineRecordPo.builder);
    }

    public static boolean replace(List<MatchOnlineRecordPo> poList) {
        List<Object[]> list = new ArrayList<>();
        for (MatchOnlineRecordPo po : poList) {
            Object[] objects = new Object[]{po.getPlayerCode(), po.getCode(), po.getInfoA(), po.getInfoB(), po.isWin(), po.getBattleResult(), po.getDate(),
                    po.getRecordCodes(), po.getSyncdb(), po.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == poList.size();
    }

    public static void delete(String pCode, long updateTime) {
        DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
    }
}
